<?php 
    //在 点击登陆的时候收集用户的输入信息
    
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Sign in &laquo; Admin</title>
  <link rel="stylesheet" href="../static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../static/assets/css/admin.css">
</head>
<body>
  <div class="login">
    <form class="login-wrap">
      <img class="avatar" src="../static/assets/img/default.png">
      <!-- 有错误信息时展示 -->
      <div class="alert alert-danger" style="display:none">
        <strong>错误！</strong> 用户名或密码错误！
      </div>
      <div class="form-group">
        <label for="email" class="sr-only">邮箱</label>
        <input id="email" type="email" class="form-control" placeholder="邮箱" autofocus>
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input id="password" type="password" class="form-control" placeholder="密码">
      </div>
      <a class="btn btn-primary btn-block" id="login" href="javascript:;">登 录</a>
    </form>
  </div>
</body>
<script src="../static/assets/vendors/jquery/jquery.min.js"></script>
<script>
    //点击登录
    $("#login").on("click",function(){
        // alert(123);
        //收集用户输入的信息
        //获取邮箱和密码
        var email = $("#email").val();
        var pwd = $("#password").val();
        // console.log(email);
        //判断邮箱和密码的格式是否正确
        var emailReg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        if(!emailReg.test(email)){
            $('.alert').show().text("邮箱格式错误!");
            return;
        }
        var pwdReg = /\w{6,10}/;
        if(!pwdReg.test(pwd)){
            $('.alert').show().text("密码错误!");
            return;
        }
        //如果正确 就发送ajax把邮箱和密码发送到后台
        $.ajax({
            type:"post",
            url:"api/_userLogin.php",
            data:{
               "email":email,
               "password":pwd
            },
            success:function(res){
               console.log(res);
                //判断是不是登录成功
                if(res.code == 1){
                    alert("恭喜你,登录成功!");
                    //如果登录成功,则跳转到登录首页
                    location.href = "index.php";
                }else{
                  $('.alert').show().text("用户名或密码错误!");
                }
            }
        });
    });
    
    
    
    //后台
    //接收ajax发来的请求:邮箱和密码
    //查询数据库 判断是否有这个用户输入的邮箱和密码
    //如果有,则登录成功
    //如果没有 返回失败
</script>
</html>
